package com.poker.controller;

import com.poker.consts.Consts;
import com.poker.entity.ActivityOuts;
import com.poker.service.ActivityOutsService;
import com.poker.util.Misc;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;

/**
 * @ClassName ActivityOutsController
 * @Description TODO
 * @Author duyuanyuan
 * @Date 2019/3/13 13:43
 * @Version 1.0
 **/
@Controller
@RequestMapping("/activityOuts")
public class ActivityOutsController extends BaseController{

    @Resource
    private ActivityOutsService activityOutsService;
    /*
     * 功能描述:分页查询赛况信息
     * @param:  * @param model
     * @param page
     * @param size
     * @return: java.lang.String
     * @auther: duyuanyuan
     * @date: 2019/3/18 10:52
     */
    @RequestMapping("/list")
    public String list(Model model,
                       @RequestParam(value = "page", defaultValue = "0") Integer page,
                       @RequestParam(value = "size", defaultValue = "50") Integer size){
        Integer managerInfo = getManagerInfo();
        Page<ActivityOuts> data = activityOutsService.findList(page,size,managerInfo);
        model.addAttribute("activityOuts", new ActivityOuts());
        model.addAttribute("data", data);
        return managerPage("activityOuts/list");
    }
    /*
     * 功能描述:分页加条件查询赛况信息列表
     * @param:  * @param model
     * @param activityOuts
     * @param page
     * @param size
     * @return: java.lang.String
     * @auther: duyuanyuan
     * @date: 2019/3/18 10:52
     */
    @RequestMapping("/findQuery")
    public String findQuery(Model model,ActivityOuts activityOuts,
                       @RequestParam(value = "page", defaultValue = "0") Integer page,
                       @RequestParam(value = "size", defaultValue = "50") Integer size){
        Integer managerInfo = getManagerInfo();
        Page<ActivityOuts> data = activityOutsService.findListAll(page,size,activityOuts,managerInfo);
        model.addAttribute("activityOuts", activityOuts);
        model.addAttribute("data", data);
        return managerPage("activityOuts/list");
    }
    /*
     * 功能描述:查询赛况信息返回修改页面
     * @param:  * @param model
     * @param id
     * @param editType
     * @return: java.lang.String
     * @auther: duyuanyuan
     * @date: 2019/3/18 10:53
     */
    @RequestMapping("/edit")
    public String edit(Model model, String id, String editType) {
        ActivityOuts activityOuts = new ActivityOuts();
        if (Consts.EditType.EDIT.equals(editType)) activityOuts = activityOutsService.getById(id);

        model.addAttribute("activityOuts", activityOuts);
        model.addAttribute("editType", editType);
        return managerPage("activityOuts/edit");
    }

    @RequestMapping("/addOuts")
    public String addOuts(Model model,String id,String editType){
        ActivityOuts activityOuts = new ActivityOuts();
        activityOuts.setActivityId(id);
        model.addAttribute("activityOuts", activityOuts);
        model.addAttribute("editType", editType);
        return managerPage("activityOuts/edit");
    }
    /*
     * 功能描述:修改赛况信息提交
     * @param:  * @param activityOuts
     * @return: java.lang.String
     * @auther: duyuanyuan
     * @date: 2019/3/18 10:53
     */
    @ResponseBody
    @RequestMapping("/doEdit")
    public String doEdit(ActivityOuts activityOuts) {
        if (Misc.isEmpty(activityOuts) || Misc.isEmpty(activityOuts.getId())) toJson(false, "编辑信息为空", null);
        activityOutsService.update(activityOuts);
        return toJson(true, "success", null);
    }
    /*
     * 功能描述:赛况信息新增
     * @param:  * @param activityOuts
     * @return: java.lang.String
     * @auther: duyuanyuan
     * @date: 2019/3/18 10:54
     */
    @ResponseBody
    @RequestMapping("/add")
    public String add(ActivityOuts activityOuts) {
        if (Misc.isEmpty(activityOuts)) return toJson(false, "添加信息为空", null);
        activityOutsService.saveActivityOuts(activityOuts);
        return toJson(true, "success", null);
    }
    /*
     * 功能描述:删除赛况信息
     * @param:  * @param id
     * @param page
     * @return: java.lang.String
     * @auther: duyuanyuan
     * @date: 2019/3/18 10:54
     */
    @RequestMapping("/delete")
    public String delete(String id, Integer page) {
        if (Misc.isEmpty(id)) return toJson(false, "id信息为空", null);

        if (Misc.isEmpty(page)) return toJson(false, "page信息为空", null);

        ActivityOuts outs = activityOutsService.getById(id);
        if (Misc.isEmpty(outs)) return toJson(false, "目标信息不存在", null);

        activityOutsService.deleteActivityOuts(outs);

        return redirect("/activityOuts/list");
    }
}
